/**
 *   类说明
 * 
 *   @creator         xldc	497937995@qq.com  
 *   @create-time     2014年1月14日   下午6:16:55   
 */
package com.huacheng.cms.service;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.huacheng.cms.constant.ArticleConstant;
import com.huacheng.cms.dao.ArticleMapper;
import com.huacheng.cms.dao.FolderMapper;
import com.huacheng.cms.exception.BaseException;
import com.huacheng.cms.model.Article;
import com.huacheng.cms.model.Folder;
import com.huacheng.cms.model.Tag;
import com.huacheng.cms.model.vo.ArticleVo;
import com.huacheng.cms.model.vo.PageVo;

@Service
public class ArticleService {

	@Autowired
	private ArticleMapper articleM;

	@Autowired
	private FolderMapper folderM;

	@Autowired
	private TagService tagS;

	public ArticleVo addOne(long folderId) {
		ArticleVo artVo = new ArticleVo();
		artVo.setFolderid(folderId);
		artVo.setCreatetime(new Date());
		articleM.insertSelective(artVo);
		return articleM.selectByPrimaryKey(artVo.getArticleid());
	}

	public Article addOne(long folderId, String name, String content, String tag, String thumbpic,
			ArticleConstant.Status status) {
		Article art = new Article();
		art.setFolderid(folderId);
		art.setContent(content);
		art.setThumbpic(thumbpic);
		art.setStatus(status.toString());
		art.setCreatetime(new Date());
		art.setVisit(0);

		articleM.insertSelective(art);
		// 验证，插入之后能够获取articleId
		// System.out.println(JSON.toJSONStringWithDateFormat(art,
		// "yyyy-MM-dd HH:mm:ss"));

		if (folderId != 0) {
			Folder folder = folderM.selectByPrimaryKey(folderId);
			folder.setArticlecount(folder.getArticlecount() + 1);
			folderM.updateByPrimaryKeySelective(folder);
		}

		tagS.add(tag, art.getArticleid());

		return articleM.selectByPrimaryKey(art.getArticleid());
	}

	public ArticleVo getOneByKey(long articleid) {
		ArticleVo artVo = articleM.selectByPrimaryKey(articleid);
		artVo.setTag(tagS.getTagsOfArticleId(artVo.getArticleid()));
		return artVo;
	}

	public int deleteOneByKey(long articleid) {
		Article article = articleM.selectByPrimaryKey(articleid);
		if ((article != null) && (article.getFolderid() != 0)) {
			Folder folder = folderM.selectByPrimaryKey(article.getFolderid());
			folder.setArticlecount(folder.getArticlecount() - 1);
			folderM.updateByPrimaryKeySelective(folder);
		}
		tagS.delete(article.getArticleid());
		return articleM.deleteByPrimaryKey(articleid);
	}

	public int deleteBySelective(Article article) {
		return articleM.deleteBySelective(article);
	}

	/**
	 * 改变某些属性，如status,visit
	 * 
	 * @param article
	 */
	public void updateOneBySelective(Article article) {
		articleM.updateByPrimaryKeySelective(article);
	}

	public ArticleVo updateOneByObj(ArticleVo articleVo) {
		articleVo.setUpdatetime(new Date());
		articleM.updateByPrimaryKeySelective(articleVo);

		if (articleVo.getFolderid() != 0) {
			Folder folder = folderM.selectByPrimaryKey(articleVo.getFolderid());
			folder.setArticlecount(folder.getArticlecount() + 1);
			folderM.updateByPrimaryKeySelective(folder);
		}

		tagS.add(articleVo.getTag(), articleVo.getArticleid());
		return articleVo;
	}

	/**
	 * 更新内容
	 * 
	 * @param articleId
	 * @param folderId
	 * @param name
	 * @param content
	 * @param tag
	 * @param thumbpic
	 * @param status
	 * @return
	 */
	public ArticleVo updateOneByObj(long articleId, long folderId, String name, String content, String tag,
			String thumbpic, ArticleConstant.Status status) {
		ArticleVo artVo = new ArticleVo();
		artVo.setArticleid(articleId);
		artVo.setFolderid(folderId);
		artVo.setName(name);
		artVo.setContent(content);
		artVo.setTag(tag);
		if (!thumbpic.equals("")) {
			artVo.setThumbpic(thumbpic);
		}
		artVo.setStatus(status.toString());
		artVo.setUpdatetime(new Date());
		articleM.updateByPrimaryKeySelective(artVo);

		if (folderId != 0) {
			Folder folder = folderM.selectByPrimaryKey(folderId);
			folder.setArticlecount(folder.getArticlecount() + 1);
			folderM.updateByPrimaryKeySelective(folder);
		}

		tagS.add(tag, artVo.getArticleid());
		return artVo;
	}

	public int getListCountByObj(Article article) {
		return articleM.selectBySelectiveCount(article);
	}

	public List<ArticleVo> getListByObj(Article article, long offset, long rows) {
		return articleM.selectBySelective(article, offset, rows);
	}

	/**
	 * 获取目录下某状态的所有文章列表，当目录Id为0时，获取所有目录，非0时，获取指定目录
	 * 
	 * @param folderId
	 * @param status
	 * @param pageNum
	 * @return
	 * @throws BaseException
	 */
	public PageVo<ArticleVo> getArticlePageByTypeAndStatusPage(long folderId, ArticleConstant.Status status, int pageNum)
			throws BaseException {
		PageVo<ArticleVo> pageVo = new PageVo<ArticleVo>(pageNum);
		pageVo.setRows(20);
		pageVo.getArgs().put("status", status.toString());
		Article art = new Article();
		// 目录Id非0时设置，反之不设置就不查找
		if (folderId != 0) {
			art.setFolderid(folderId);
		}
		art.setStatus(status.toString());

		pageVo.setList(getListByObj(art, pageVo.getOffset(), pageVo.getRows()));
		pageVo.setCount(getListCountByObj(art));
		return pageVo;
	}

	/**
	 * 获取相同标签的所有文章列表分页
	 * 
	 * @param tagName
	 * @param pageNum
	 * @return
	 */
	public PageVo<ArticleVo> getArticlesByTag(String tagName, int pageNum) {
		PageVo<ArticleVo> pageVo = new PageVo<ArticleVo>(pageNum);
		pageVo.setRows(20);
		Tag tag = tagS.selectByPrimaryKey(tagName);
		String artIds = tag.getArtids();
		artIds = artIds.substring(1, artIds.length() - 1);
		artIds.replace("|", ",");
		List<ArticleVo> artVo_list = articleM.selectByPrimaryKeys(artIds);
		pageVo.setList(artVo_list);
		pageVo.setCount(artVo_list.size());
		return pageVo;
	}
}
